<template>
  <el-dialog :title="'调整项目级别'" :visible.sync="visible" v-dialogDrag :close-on-click-modal="false" @open="open" @close="close" class="tl-dialog" width="1000px" :append-to-body="true" :modal-append-to-body="true">
    <el-row>
      <el-col :span="24">
          <h4 style="margin-left: 35px">
            根据《鼎信通讯流程建设与落地质量政策》，该项目属于
            <span style="color: red">{{dispprojectdefaultlevel}}</span>
            级项目。如需调整项目级别，请说明理由。
          </h4>
      </el-col>
    </el-row>
    <el-form class="tl-form" :model="model" :rules="rules" ref="form" label-width="100px">
      <el-row>
				<el-col :span="24">
					<el-form-item label="项目级别" prop="projectlevel" :rules="rules.validateRequire">
            <el-select v-model="model.projectlevel" @change="changeSelect" filterable clearable>
              <el-option v-for="item in projectlevelList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
				</el-col>
			</el-row>
      <el-row v-if="needReason">
        <el-col :span="24">
          <el-form-item label="调整原因" prop="changelevelreason" :rules="rules.validateRequire">
            <el-input v-model="model.changelevelreason" type="textarea" :rows="4" maxlength="1000" show-word-limit>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSubmitEdit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>

/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [],
  components: { },
  data() {
    return {
      visible: false,
      model: {},
      projectlevelList: [],
      dispprojectdefaultlevel: '',
      rules: {
        validateRequire: [
          // 必填
          { required: 'true', message: '请输入内容', trigger: 'change' }
        ]
      },
      needReason: false
    }
  },
  methods: {
    open() {
      // 若选择的等级和默认等级相同，则不需要填写原因
      if (this.model.dispprojectlevel === this.dispprojectdefaultlevel) {
        this.needReason = false
        this.$set(this.model, 'changelevelreason', '')
      } else {
        this.needReason = true
      }
    },
    close() {
      this.model = {}
      this.needReason = false
    },
    doCancelEdit() {
      this.visible = false
    },
    doSubmitEdit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('confirm', this.model)
          this.visible = false
        } else {
          return false
        }
      })
    },
    changeSelect(data) {
      let selectedData = this.projectlevelList.find(item => item.code === data)
      if (selectedData) {
        // 若选择的等级和默认等级相同，则不需要填写原因
        if (selectedData.name === this.dispprojectdefaultlevel) {
          this.needReason = false
          this.model.changelevelreason = ''
        } else {
          this.needReason = true
        }
        this.$set(this.model, 'dispprojectlevel', selectedData.name)
      }
    }
  }
}
</script>

